#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

int BubbleSort(int A[], int n) {
    int M = 0, C = 0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            C++;
            if (A[j] > A[j + 1]) {
                swap(A[j], A[j + 1]);
                M+=3;
            }
        }
    }
    return M+C;
}

int ShakerSort(int A[], int n){
    int L = 0, R = n-1, K = n-1;
    int M = 0, C = 0;
    while(L < R) {
        for(int j = R; j > L; j--){
            C++;
            if(A[j] < A[j-1]){
                M+=3;
                swap(A[j], A[j-1]);
                K = j;
            }
        }
        L = K;
        for(int j = L; j < R; j++){
            C++;
            if(A[j] > A[j+1]){
                M+=3;
                swap(A[j], A[j+1]);
                K = j;
            }
        }
        R = K;
    }
    return M+C;
}

int SelectSort(int A[], int n) {
    int k = 0, temp, M = 0, C = 0;
    for (int i = 0; i < n - 1; i++) {
        k = i;
        for (int j = i + 1; j < n; j++) {
            C++;
            if (A[j] < A[k]) {
                k = j;
            }
        }
        if (k != i) {
            swap(A[i], A[k]);
            M+=3;
        }
    }
    return M+C;
}

int InsertSort(int A[], int n){
    int temp, j;
    int M = 0, C = 0;
    for(int i = 1; i < n; i++){
        temp = A[i]; j = i - 1;
        while(j >= 0 && temp < A[j]){
            A[j+1] = A[j];
            j = j - 1;
            M++;
        }
        A[j+1] = temp;
        C++;
    }
    return M+C;
}

void PrintMas(int A[], int n) {
    for (int i = 0; i < n; i++) {
        cout << A[i] << " ";
    }
    cout << endl;
}

void FillRand(int A[], int n) {
    for (int i = 0; i < n; i++) {
        A[i] = rand();
    }
}

int main(){
    int n = 100;
    int *A;
    A = (int *)malloc(sizeof(int)*n);
    srand(time(NULL));
    
    int k_Select, k_Bubble, k_Shaker, k_Insert;
    
    cout << setw(2) << "n" << setw(15) << "Select" << setw(15) << "Bubble" << setw(15) << "Shaker" << setw(15) << "Insert" << endl;
    
    n = 100;
    FillRand(A, n);
    k_Select = SelectSort(A, n);
    FillRand(A, n);
    k_Bubble = BubbleSort(A, n);
    FillRand(A, n);
    k_Shaker = ShakerSort(A, n);
    FillRand(A, n);
    k_Insert = InsertSort(A, n);
    
    cout << n << setw(13) << k_Select << setw(15) << k_Bubble << setw(14) << k_Shaker << setw(15) << k_Insert << endl;
    delete A;
    
    n = 200;
    A = (int *)malloc(sizeof(int)*n);
    FillRand(A, n);
    k_Select = SelectSort(A, n);
    FillRand(A, n);
    k_Bubble = BubbleSort(A, n);
    FillRand(A, n);
    k_Shaker = ShakerSort(A, n);
    FillRand(A, n);
    k_Insert = InsertSort(A, n);
    
    cout << n << setw(14) << k_Select << setw(15) << k_Bubble << setw(14) << k_Shaker << setw(15) << k_Insert << endl;
    delete A;
    
    n = 300;
    A = (int *)malloc(sizeof(int)*n);
    FillRand(A, n);
    k_Select = SelectSort(A, n);
    FillRand(A, n);
    k_Bubble = BubbleSort(A, n);
    FillRand(A, n);
    k_Shaker = ShakerSort(A, n);
    FillRand(A, n);
    k_Insert = InsertSort(A, n);
    
    cout << n << setw(14) << k_Select << setw(15) << k_Bubble << setw(14) << k_Shaker << setw(15) << k_Insert << endl;
    delete A;
    
    n = 400;
    A = (int *)malloc(sizeof(int)*n);
    FillRand(A, n);
    k_Select = SelectSort(A, n);
    FillRand(A, n);
    k_Bubble = BubbleSort(A, n);
    FillRand(A, n);
    k_Shaker = ShakerSort(A, n);
    FillRand(A, n);
    k_Insert = InsertSort(A, n);
    
    cout << n << setw(14) << k_Select << setw(15) << k_Bubble << setw(14) << k_Shaker << setw(15) << k_Insert << endl;
    delete A;
    
    n = 500;
    A = (int *)malloc(sizeof(int)*n);
    FillRand(A, n);
    k_Select = SelectSort(A, n);
    FillRand(A, n);
    k_Bubble = BubbleSort(A, n);
    FillRand(A, n);
    k_Shaker = ShakerSort(A, n);
    FillRand(A, n);
    k_Insert = InsertSort(A, n);
    
    cout << n << setw(14) << k_Select << setw(15) << k_Bubble << setw(14) << k_Shaker << setw(15) << k_Insert << endl;
}

